var a = getApp();

Page({
    data: {
        baseUrl: a.globalData.baseUrl,
        act: {},
        num: 1,
        totalPay: 0,
        userId: a.globalData.userId
    },
    onLoad: function(t) {
        var e = this;
        if (e.setData({
            id: t.id,
            type: t.type,
            orderId: t.orderid ? t.orderid : ""
        }), 3 == e.data.type) this.getinitDatakan(); else {
            var i = a.globalData.urlBase + a.globalData.urlactivitydetail;
            this.getinitData(i);
        }
    },
    getinitDatakan: function() {
        var t = this, e = a.globalData.baseUrl + a.globalData.urlKanDetail;
        wx.request({
            url: e,
            data: {
                orderId: t.data.orderId,
                lng: a.globalData.lng,
                lat: a.globalData.lat
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                if (0 != e.code) wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }); else {
                    e.activity.imgs.split(",");
                    for (var i = 0, o = 0; o < e.activity.userKanList.length; o++) i = (parseFloat(e.activity.userKanList[o].price) + parseFloat(i)).toFixed(2);
                    t.setData({
                        userkanPrice: i,
                        userkanNum: e.activity.userKanList.length > 0 ? e.activity.userKanList.length : 0,
                        needKanPrice: (parseFloat(e.activity.maxPrice) - parseFloat(e.activity.minPrice) - i).toFixed(2),
                        curPrice: (parseFloat(e.activity.maxPrice) - i).toFixed(2)
                    }), t.setData({
                        act: e.activity
                    });
                }
            },
            fail: function(a) {
                console.log(".....fail.....");
            }
        });
    },
    getinitData: function(a) {
        var t = this;
        wx.request({
            url: a,
            data: {
                activityId: t.data.id,
                type: t.data.type
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                console.log(e), 0 != e.code ? wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }) : (t.setData({
                    act: e.activity,
                    type: t.data.type,
                    totalPay: e.activity.price
                }), 3 == t.data.type && t.setData({
                    totalPay: e.activity.maxPrice
                }));
            }
        });
    },
    mulNum: function() {
        var a = this, t = a.data.num, e = a.data.act.price;
        t <= 1 ? t = 1 : t -= 1, a.setData({
            num: t,
            totalPay: t * e
        });
    },
    addNum: function() {
        var a = this, t = a.data.num, e = a.data.act.price;
        t >= 99 ? t = 99 : t += 1, a.setData({
            num: t,
            totalPay: t * e
        });
    },
    toPay: function(a) {
        var t = this;
        3 == t.data.type ? t.getPayData(t.data.orderId) : t.createOrder();
    },
    createOrder: function() {
        var t = this, e = t.data.id, i = t.data.num, o = (a.globalData.addressId, a.globalData.urlBase + a.globalData.urlAddActivityOrder);
        wx.request({
            url: o,
            data: {
                activityId: e,
                num: i,
                userId: a.globalData.userId,
                type: t.data.type,
                orderId: t.data.orderId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                0 != e.code ? wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 500
                }) : t.getPayData(e.msg);
            }
        });
    },
    getPayData: function(t) {
        var e = this, i = a.globalData.urlBase + a.globalData.urlPayData;
        wx.request({
            url: i,
            data: {
                orderId: t,
                userId: a.globalData.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var t = JSON.parse(a.data);
                0 != t.code ? wx.showToast({
                    title: t.msg,
                    icon: "none",
                    duration: 500
                }) : t.price > 0 ? e.wxPay(t) : wx.navigateTo({
                    url: "../myCollist/myCollist?type=" + e.data.type
                });
            }
        });
    },
    wxPay: function(a) {
        var t = this;
        wx.requestPayment({
            timeStamp: a.timeStamp,
            nonceStr: a.nonceStr,
            package: "prepay_id=" + a.totalId,
            signType: "MD5",
            paySign: a.sign,
            success: function(a) {
                console.log(a), wx.navigateTo({
                    url: "../myCollist/myCollist?type=" + t.data.type
                });
            },
            fail: function(a) {
                wx.showToast({
                    title: "支付失败",
                    icon: "none",
                    duration: 500,
                    complete: function() {
                        wx.navigateTo({
                            url: "../myCollist/myCollist?type=" + t.data.type
                        });
                    }
                });
            },
            complete: function(a) {}
        });
    },
    onShareAppMessage: function() {}
});